/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.constant.CommonConstants;
import com.ykht.proj.common.core.exception.BusinessException;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.MerchantTypeEnum;
import com.ykht.proj.coupon.entity.CnCouponLog;
import com.ykht.proj.coupon.entity.FxBrandMoneyFlow;
import com.ykht.proj.coupon.entity.FxMerchantAccount;
import com.ykht.proj.coupon.mapper.FxBrandMoneyFlowMapper;
import com.ykht.proj.coupon.service.FxBrandMoneyFlowService;
import com.ykht.proj.coupon.service.FxMerchantAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 品牌账户资金流水
 *
 * @author proj code generator
 * @date 2021-08-18 18:18:54
 */
@Service
public class FxBrandMoneyFlowServiceImpl extends ServiceImpl<FxBrandMoneyFlowMapper, FxBrandMoneyFlow> implements FxBrandMoneyFlowService {

	@Autowired
	private FxMerchantAccountService fxMerchantAccountService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void Recharge(FxBrandMoneyFlow flow) {
		FxMerchantAccount account = fxMerchantAccountService.cheeckOrCreateAccont(MerchantTypeEnum.brand.getValue(), flow.getBrandId());
		String batchNumber = System.currentTimeMillis()+"";
		flow.setBatchNumber(batchNumber);
		flow.setFlowType(0);//0充值1提现
		flow.setBalanceMoney(account.getBalance().add(flow.getFlowMoney()));
		flow.setOrderId(batchNumber);
		flow.setTradeId(batchNumber);
//		flow.setWay(2);//充值方式：0线下微信转账1支付宝2后台3银行卡
		flow.setCreateUser(SecurityUtils.getUserId());
		flow.setCreateTime(LocalDateTime.now());
		flow.insert();
		fxMerchantAccountService.updateMerchantAccount(MerchantTypeEnum.brand.getValue(),flow.getBrandId(),flow.getFlowMoney(),flow.getFlowMoney(),null,null,null,flow.getFlowMoney(),SecurityUtils.getUserId());
	}

}
